<template>
  <el-card>
    <div class="mb20" slot="header">商品添加</div>
    <el-form
      :model="ruleForm"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="商品名称" prop="name">
        <el-input v-model="ruleForm.name" placeholder="商品名称"></el-input>
      </el-form-item>
      <el-form-item label="商品分类" prop="category">
        <el-select v-model="ruleForm.category" placeholder="请选择商品分类">
          <el-option label="类型一" value="类型一"></el-option>
          <el-option label="类型二" value="类型二"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="商品价格" required class="price">
        <el-input-number
          v-model="ruleForm.price"
          :min="1"
          :max="10"
        ></el-input-number>
      </el-form-item>
      <el-form-item label="图片上传" required>
        <!-- action上传图片服务器地址 -->
        <!-- show-file-list 展示上传列表 -->
        <el-upload
          class="avatar-uploader"
          :action="`${API}/goods/goods_img_upload`"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
        >
          <img v-if="ruleForm.imgUrl" :src="ruleForm.imgUrl" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>

      <el-form-item label="商品描述" prop="goodsDesc">
        <el-input type="textarea" v-model="ruleForm.goodsDesc"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">
          立即创建
        </el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
import {GoodsAddReq} from '@/api/goods'
import {mapState} from 'vuex'
export default {
  computed: {
    ...mapState('user', ['goodsInfo']),
  },
  data() {
    return {
      API: 'http://sell.h5.itsource.cn:8087',
      ruleForm: {
        name: '',
        category: '',
        price: '',
        imgUrl: '',
        goodsDesc: '',
      },
    }
  },
  methods: {
    // 添加商品
    submitForm(formName) {
      this.$refs[formName].validate(async valid => {
        if (valid) {
          let res = await GoodsAddReq({
            ...this.ruleForm,
          })
          // 解构数据
          let {code} = res.data
          // 判断
          if (code === 0) {
            console.log('成功')
            // 跳转
            this.$router.push('/goods/goods-list')
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    //上传成功触发的函数
    async handleAvatarSuccess(res) {
      let {code, imgUrl} = res
      if (code === 0) {
        this.ruleForm.imgUrl = imgUrl
      }
      //解构数据
      if (code === 0) {
        // 发送请求
        let res2 = await GoodsAddReq({
          ...this.ruleForm,
          imgUrl,
        })
        // 解构数据
        let {code} = res2.data
        // 判断
        if (code === 0) {
          console.log('成功')
          // 跳转
          this.$router.push('/goods/goods-list')
        }
      }
    },
  },
  handleAvatarSuccess(res, file) {
    this.imageUrl = URL.createObjectURL(file.raw)
  },
  // 图片上传之前
  beforeAvatarUpload(file) {
    const isJPG = /(png|jpeg)/.test(file.type)
    const isLt2M = file.size / 1024 / 1024 < 2

    if (!isJPG) {
      this.$message.error('上传头像图片只能是 PNG JPG 格式!')
    }
    if (!isLt2M) {
      this.$message.error('上传头像图片大小不能超过 2MB!')
    }
    return isJPG && isLt2M
  },
}
</script>

<style lang="scss" scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 30px;
  color: #8c939d;
  width: 150px;
  height: 150px;
  line-height: 150px;
  text-align: center;
  border: 1px solid gray;
}
.avatar {
  width: 150px;
  height: 150px;
  display: block;
}
:deep(.el-input__inner) {
  width: 180px;
}
:deep(.el-textarea) {
  max-width: 300px;
}
</style>
